<template>
  <el-input class="text-editor" v-model="innerText" type="textarea" size="small" @input="change" @focus="hasChange = false" @blur="blur" :style="{ left: `${edit.left}px`, top: `${edit.top}px`, width: `${edit.width}px`, height: `${edit.height}px` }" />
</template>

<script>
export default {
  name: "TextareaContent",
  props: ["modelValue", "edit"],
  emits: ["update:modelValue", "edit-blur"],

  data() {
    return {
      hasChange: true,
    };
  },

  mounted() {
    this.$nextTick(() => {
      this.$el.focus();
    });
  },

  computed: {
    innerText: {
      get() {
        return this.modelValue;
      },
      set(val) {
        if (this.hasChange) {
          this.$emit("update:modelValue", val);
        }
      },
    },
  },

  methods: {
    change(value) {
      this.$emit("update:modelValue", value);
    },

    blur(value) {
      this.hasChange = true;
      this.$emit("edit-blur", value);
    },
  },
};
</script>

<style lang="scss" scoped>
.text-editor {
  position: absolute;
  z-index: 300;
  min-width: 60px;
  min-height: 80px;
  ::v-deep(.el-textarea__inner) {
    padding: 4px 6px;
    height: 100%;
    width: 100%;
    border: none;
    outline: none;
    resize: none;
    font-size: 12px;
    line-height: 1.5;
    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  }
}
</style>
